// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Saturday, May 07, 2011</created>
// <lastedit>Saturday, May 07, 2011</lastedit>
// ===================================

using System;
using System.Ext;
using LABSMFController.LABS.Classses;

namespace LABSMFController.LABS.Services
{
    internal class LoggingService : BeerService
    {
        //public void Log(string message)
        //{
        //    if (Settings.GetSetting("LogToConsole").Equals("1"))
        //        Console.Write(message);


        //    //System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
        //   // Console.Write(message);
        //   // BeerProgram.Log(message);

        //   // if (BeerDevices.eventService != null)
        //    //    BeerDevices.eventService.LoggingEvent(new LoggingEvent{message = message});
        //}

        private static object locker;

        private static int currentQ;
        private static int MaxQ = 12;
        private static bool isReading;

        private readonly string[] quelogs = new string[MaxQ];

        public bool HaveQ
        {
            get
            {
                lock (locker)
                {
                    if (currentQ == MaxQ)
                    {
                        return false;
                    }
                    return true;
                }
            }
        }

        public override void Configure()
        {
            currentQ = MaxQ;
            locker = new object();
            Console.Write("Logging configure");
        }

        public override void Start()
        {
            Console.Write("Logging start");
        }

        public override void Stop()
        {
            Console.Write("Logging stop");
        }

        public string GetQue()
        {
            lock (locker)
            {
                if (currentQ == MaxQ) return "";

                return quelogs[currentQ++];
            }
        }

        public string SendAllLogs()
        {
            lock (locker)
            {
                isReading = true;
                string ret = "HEADER:Logging:Message;";
                for (int c = MaxQ - 1; c >= currentQ; c = c - 1)
                {
                    ret = ret + "\r\n" + quelogs[c] + ";";
                }
                isReading = false;
                currentQ = MaxQ;
                return ret;
            }
        }

        private void AddToQ(string log)
        {
            lock (locker)
            {
                currentQ = currentQ - 1;

                if (currentQ == -1)
                {
                    for (int c = MaxQ - 1; c > 0; c = c - 1)
                    {
                        quelogs[c] = quelogs[c - 1];
                    }
                    currentQ = 0;
                }

                quelogs[currentQ] = log;
            }
        }

        public void Log(string message, int loggingSeverity)
        {
            // BeerProgram.SendToClients(message);
            if (message.Substring(0, 5).Equals("PANIC"))
            {
                Console.Write(DateTime.Now + " : " + message);
                AddToQ(DateTime.Now + " : " + message);
                return;
            }

            if (loggingSeverity > Convert.ToInt32(Settings.GetSetting("LoggingSeverity")))
            {
                if (Settings.GetSetting("LogToConsole").Equals("1"))
                    Console.Write(DateTime.Now + " : " + message);
            }

            //System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
            // Console.Write(message);
            // BeerProgram.Log(message);

            // if (BeerDevices.eventService != null)
            //    BeerDevices.eventService.LoggingEvent(new LoggingEvent{message = message});
        }
    }
}